<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 品牌model层
 * @author Toby.Tu 2016-09-02
 */
class Category_model extends My_Model{
    public $tab = 'category';
    public $pk = 'id';
    /**
     * 获取用户列表
     * @author Toby.tu 2016-09-01
     */
    public function getcategoryList($search=array(),$current_page=1,$pagesize=0){
        if(isset($search['is_admin']) && 'yes' == $search['is_admin']){
            $field = 'category.*,brand.name as brand_name';
            $query = $this->select($field)->join('brand','brand.id=category.brand_id','left');
        }else{
            $field = 'category.*';
            $query = $this->select($field);
        }
        $query->where('category.delete_id',0);
        if(isset($search['name'])){//品牌故事名称
            $query->like('category.name',$search['name']);
        }
        if(isset($search['brand_id']) && $search['brand_id'] > 0){//品牌
            $query->where('category.brand_id',$search['brand_id']);
        }
        if(isset($search['status'])){//状态
            $query->where('category.status',$search['status']);
        }
        $query->order_by('category.parent_id');

        // if(empty($pagesize)){
        //     $pagesize = $this->pagesize;
        // }
        // $start = ($current_page-1)*$pagesize;
        // $query->limit($pagesize,$start);
        $rows = $this->get();
        if(!empty($rows)){
            return $rows;
        }
        return array();
    }
    /**
     * 获取品牌故事总数
     * @author Toby.tu 2016-09-01
     */
    public function getcategoryCount($search=array()){
        $tab = $this->getTabName();
        $field = 'count('.$tab.'.id) as user_count';
        $query = $this->select($field)->where('delete_id',0);
        if(isset($search['name'])){//品牌故事名称
            $query->like('name',$search['name']);
        }
        if(isset($search['brand_id']) && $search['brand_id'] > 0){//品牌
            $query->where('brand_id',$search['brand_id']);
        }
        if(isset($search['status'])){//状态
            $query->where('status',$search['status']);
        }
        $query->order_by('category.id desc');
        $row = $this->getRow();
        if(!empty($row)){
            return $row['user_count'];
        }
        return 0;
    }
    /**
     * 获取用户列表
     * @author Toby.tu 2016-09-01
     */
    public function getCategoryByParent($search=array()){
        $field = 'category.*';
        $query = $this->select($field);
        $query->where('category.delete_id',0);
        if(isset($search['brand_id']) && $search['brand_id'] > 0)
            $query->where('category.brand_id',$search['brand_id']);
        if(isset($search['parent_id']) && $search['parent_id'] > 0)
            $query->where('category.parent_id',$search['parent_id']);
        $query->order_by('category.parent_id');
        $rows = $this->get();
        if(!empty($rows)){
            return $rows;
        }
        return array();
    }
    /**
     * 获取品牌对应的商品类别
     * @author Toby.tu 2017-03-08
     */
    public function getCategoryByBrandId($brand_id=0){
        $field = '*';$tab = $this->getTabName();
        $query = $this->db->select($field)->where('delete_id',0);
        if($brand_id > 0){
            $query->where('brand_id',$brand_id);
        }
        $query->where('level < 3');//只取前两级
        $result = $query->order_by('parent_id')->get($tab);
        if(!empty($result)){
            return $result->result_array();
        }
        return array();
    }
}
